﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using BLL;

namespace Web.Shalo.BanPMS1.Admin
{
    public partial class QLNhanVien : System.Web.UI.Page
    {
        private string manv ="";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["manv"] == null || Session["matkhau"] == null)
            {
                Response.Redirect("Default.aspx");
            }
            else
            {
                if (!IsPostBack)
                {
                    MultiView1.ActiveViewIndex = 0;
                    HTNhanVien();
                }
            }
        }
        public void HTNhanVien()
        {
            NhanVienController obj=new NhanVienController();
            GVNhanVien.DataSource = obj.bllHienThiNV(manv);
            GVNhanVien.DataBind();
        }

        public void ThongBao(bool res)
        {
            if (res)
            {
                Response.Redirect("QLNhanVien.aspx");
                MultiView1.ActiveViewIndex = 0;
            }
            else
            {
                Page.RegisterClientScriptBlock("mes", "<script language='javascript'>alert('Thất Bại')</script>");
            }
        }

        protected void btnThemNV_Click(object sender, EventArgs e)
        {
            bool res = false;
            NhanVienController obj = new NhanVienController();
            string ngaysinh=ddThang.SelectedItem.ToString()+"/"+ ddNgay.SelectedItem.ToString()+"/"+ txtNam.Text;
            string matkhau = ddNgay.SelectedItem.ToString() + ddThang.SelectedItem.ToString() + txtNam.Text;
            int gioitinh = Convert.ToInt16(ddGioiTinh.SelectedValue.ToString());
            res = obj.bllThemNV(txtTennv.Text, ngaysinh, gioitinh, txtDiachi.Text, txtSdt.Text, txtEmail.Text, matkhau);
            ThongBao(res);
          
        }

        protected void btnTaoMoiNV_Click(object sender, EventArgs e)
        {
            MultiView1.ActiveViewIndex = 1;
            btnSua.Visible = false;
        }

        protected void GVNhanVien_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            NhanVienController obj = new NhanVienController();
            MultiView1.ActiveViewIndex = 1;
            manv = GVNhanVien.Rows[e.NewSelectedIndex].Cells[0].Text;
            DataTable NV = obj.bllHienThiNV(manv);
            txtTennv.Text = NV.Rows[0]["TenNV"].ToString();
            txtDiachi.Text = NV.Rows[0]["DiaChi"].ToString();
            txtEmail.Text = NV.Rows[0]["Email"].ToString();
            txtSdt.Text=NV.Rows[0]["Sdt"].ToString();
            string gioitinh = NV.Rows[0]["GioiTinh"].ToString();
            if (gioitinh == "Nam")
                ddGioiTinh.Text = "0";
            else
                ddGioiTinh.Text = "1";
            string ngaysinh = NV.Rows[0]["NgaySinh"].ToString();
            if (ngaysinh.Length > 0)
            {
                ddNgay.Text = ngaysinh.Substring(0, ngaysinh.Length - 8);
                ddThang.Text = ngaysinh.Substring(3, 2);
                txtNam.Text = ngaysinh.Substring(6, 4);
            }
            btnThemNV.Visible = false;
            labManv.Text = manv;
        }

        protected void btnHuy_Click(object sender, EventArgs e)
        {
            Response.Redirect("QLNhanVien.aspx");
            
        }

        protected void btnSua_Click(object sender, EventArgs e)
        {
            bool res = false;
            NhanVienController obj = new NhanVienController();
            string ngaysinh = ddThang.SelectedItem.ToString() + "/" + ddNgay.SelectedItem.ToString() + "/" + txtNam.Text;
            string matkhau = ddNgay.SelectedItem.ToString() + ddThang.SelectedItem.ToString() + txtNam.Text;
            int gioitinh = Convert.ToInt16(ddGioiTinh.SelectedValue.ToString());
            res = obj.bllSuaNV(txtTennv.Text, ngaysinh, gioitinh, txtDiachi.Text, txtSdt.Text, txtEmail.Text, labManv.Text);
            ThongBao(res);
        }

        protected void GVNhanVien_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            bool res = false;
            NhanVienController obj = new NhanVienController();
            manv = GVNhanVien.Rows[e.RowIndex].Cells[0].Text;
            res = obj.bllXoaNV(manv);
            ThongBao(res);
        }

        protected void GVNhanVien_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GVNhanVien.PageIndex = e.NewPageIndex;
            HTNhanVien();
        }

        
    }
}